/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tpgen.grammar;

import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author shanki
 */
class Optional implements Symbol {

    private double prob;
    private Symbol symbol;
    
    public Optional(Symbol symbol, double prob) {
        this.symbol = symbol;
        this.prob   = prob;
    }

    @Override
    public boolean isUsable() {
        return true;
    }

    @Override
    public Collection<Symbol> expand(Context context) {
        if (context.nextDouble() <= prob && symbol.isUsable()) {
            return symbol.expand(context);
        } else {
            return new ArrayList<Symbol>();
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Optional) {
            Optional other = (Optional)obj;
            
            return  this.prob == other.prob &&
                    this.symbol.equals(other.symbol);
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 89 * hash + (int) (Double.doubleToLongBits(this.prob) ^ (Double.doubleToLongBits(this.prob) >>> 32));
        hash = 89 * hash + (this.symbol != null ? this.symbol.hashCode() : 0);
        return hash;
    }
    
    

    @Override
    public String toString() {
        return "/opt/";
    }
    
    

    @Override
    public String toUserString() {
        return "";
    }
    
}